package com.xzqwjw.taskmanager.fragment;

import java.util.List;
import java.util.Map;

import com.xzqwjw.taskmanager.R;
import com.leaderinfo.model.CategoryUnit;
import com.leaderinfo.model.UnitNode;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class UnitFragment extends Fragment implements OnItemClickListener {

  private String strDBFile = "";
  private ListView lvUnitTree;
  private ProgressDialog objPD;
  private FragmentManager objFM;
  private UnitNode nodeRoot;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent objIT = getActivity().getIntent();
    strDBFile = objIT.getStringExtra("DBFilePath");
    objFM = getFragmentManager();
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_unit, container, false);
    if (view != null) {
      lvUnitTree = (ListView) view.findViewById(R.id.lvUnitTree);
      // �������ڵ�
      nodeRoot = new UnitNode("����ҽѧ�ߵ�ר��ѧУ", "0");
      nodeRoot.setIcon(R.drawable.ic_folder);
      nodeRoot.setRadioButton(false);
      nodeRoot.setCheckBox(false);
      new AsyncLoadDataUnit().execute();
      // getUnitData();
      lvUnitTree.setOnItemClickListener(this);
    }
    return view;
  }

  // ʵ�ֵ������itemʱ ���ᴥ����ѡ��� ����
  @Override
  public void onItemClick(AdapterView<?> avParent, View objView,
                          int intPosition, long lngID) {
    com.leaderinfo.utility.UnitNodeRBAdapter objAdapter = (com.leaderinfo.utility.UnitNodeRBAdapter) avParent
        .getAdapter();
    objAdapter.ExpandOrCollapse(intPosition);
    UnitNode objNode = objAdapter.getSeletedNode(intPosition);
    TransUnitID(objNode.getValue());
  }

  // ���ýڵ�,����ͨ��ѭ����ݹ鷽ʽ��ӽڵ�
  private void addNode(UnitNode nodeRoot, UnitNode nodeParent, int intParentID) {
    UnitNode objNode;
    CategoryUnit objUnitCategory;
    Map<String, Object> mapData;
    List<Map<String, Object>> listData = null;
    com.leaderinfo.dao.UnitCategoryDao objUnitCategoryDAO = new com.leaderinfo.dao.UnitCategoryDao(strDBFile);

    listData = objUnitCategoryDAO.getRecordsByParentID(intParentID);

    if (listData != null) {
      for (int i = 0; i < listData.size(); i++) {
        mapData = listData.get(i);
        objUnitCategory = (CategoryUnit) mapData.get("UnitCategory");
        if (mapData != null) {
          if (intParentID == 0) {
            objNode = new UnitNode(objUnitCategory.UnitName,
                objUnitCategory.AID + "");
            objNode.setParent(nodeRoot);
            objNode.setIcon(R.drawable.ic_folder);
            objNode.setRadioButton(true);
            objNode.setCheckBox(false);
            nodeRoot.add(objNode);
            intParentID = objUnitCategory.AID;
            // �ݹ����
            addNode(nodeRoot, objNode, intParentID);
          } else {
            objNode = new UnitNode(objUnitCategory.UnitName,
                objUnitCategory.AID + "");
            objNode.setParent(nodeParent);
            objNode.setIcon(R.drawable.ic_folder);
            objNode.setRadioButton(true);
            objNode.setCheckBox(false);
            nodeParent.add(objNode);
            intParentID = objUnitCategory.AID;
            // �ݹ����
            addNode(nodeRoot, objNode, intParentID);
          }
        }
      }
    }

  }

  private FragmentTransaction objFT;
  private FragmentShowList fmShowList;
  private Bundle objBundle;

  private void TransUnitID(String strUnitID) {
    // ע�⡣һ��transaction ֻ��commitһ�Σ����Բ�Ҫ�����ȫ�ֱ���
    objFT = objFM.beginTransaction();
    fmShowList = new FragmentShowList();
    objBundle = new Bundle();
    objBundle.putString("UnitID", strUnitID);
    fmShowList.setArguments(objBundle);
    objFT.replace(R.id.fmShowList, fmShowList);
    // objFT.addToBackStack(null);
    objFT.commit();
  }

  // ��һ���������������������ͼƬ��ַ
  // �ڶ��������Ǽ��صĽ��ȣ���ʱ����Ҫ����Void
  // �����������Ƿ��صĶ��������

  class AsyncLoadDataUnit extends android.os.AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
      objPD = ProgressDialog.show(getActivity(), "��ȡ����",
          "���ڶ�ȡ��λ�������ݣ����Եȣ�", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
      // ������нڵ�
      addNode(nodeRoot, null, 0);
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      com.leaderinfo.utility.UnitNodeRBAdapter adapterUnitNode = new com.leaderinfo.utility.UnitNodeRBAdapter(
          getActivity(), nodeRoot, lvUnitTree);
      adapterUnitNode.setRadioButton(false);
      adapterUnitNode.setCheckBox(false);
      adapterUnitNode.setExpandedCollapsedIcon(R.drawable.ic_tree_ex,
          R.drawable.ic_tree_ec);
      // ����Ĭ��չ������
      adapterUnitNode.setExpandLevel(2);
      lvUnitTree.setAdapter(adapterUnitNode);

      objPD.dismiss();
    }
  }

}
